package server;

import java.io.File;


/**
 * @file Resource.java
 * @version 0.3
 * @author James Dibble 10009689
 * 
 * Latest Source:
 * http://code.google.com/p/csp-server/source/browse/
 *                                         trunk/Server/src/server/Resource.java
 */
public final class Resource {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private int intUploadCount;
    private String resourceName, resourceDescription, 
                   resourceFileName, resourceDownloadName;
    private int resourceID;
    private File file;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    /**
     * Create an object that represents a file on the server.
     * 
     * @param resourceID Unique file identifier
     * @param resourceFileName  The name as the OS sees it
     * @param resourceName  The name as the Server sees it
     * @param resourceDescription  A short description of the file contents
     * @param resourceDownloadName  The name of the file the client should store
     */
    public Resource(int resourceID, 
                    String resourceFileName, 
                    String resourceName, 
                    String resourceDescription, 
                    String resourceDownloadName) {
        this.resourceName = resourceName;
        this.resourceID = resourceID;
        this.resourceFileName = resourceFileName;
        this.resourceDescription = resourceDescription;
        this.file = new File(resourceFileName);
        this.resourceDownloadName = resourceDownloadName;
        intUploadCount = 0;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    /**
     * Increment the download counter for this File.
     */
    public synchronized void UploadResourceAsString() {
        int tmp = intUploadCount;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            System.out.println("sleep interrupted");
        }
        intUploadCount = tmp + 1;
        System.out.println(String.format("%s has been downloaded %s time(s)", 
                                         this.resourceName,
                                         intUploadCount));
    }
    
    /**
     * Increment the download counter for this File.
     */
    public synchronized void UploadResourceAsFile(){
        int tmp = intUploadCount;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            System.out.println("sleep interrupted");
        }
        intUploadCount = tmp + 1;
        System.out.println(String.format("%s has been downloaded %s time(s)", 
                                         this.resourceName,
                                         intUploadCount));
    }
    
    /**
     * Get the file name.
     * 
     * @return 
     */
    public String getResourceName(){
        return this.resourceName;
    }
    
    /**
     * Get the ID of the File.
     * 
     * @return 
     */
    public int getResourceID(){
        return this.resourceID;
    }
    
    /**
     * Get the resource as a Java File.
     * 
     * @return 
     */
    public File getResourceAsFile(){
        return this.file;
    }
    
    /**
     * Get the resource file name.
     * 
     * @return 
     */
    public String getFileName(){
        return this.resourceFileName;
    }
    
    /**
     * Get the file name as the client should create.
     * 
     * @return 
     */
    public String getDownloadFileName(){
        return this.resourceDownloadName;
    }
    
    @Override
    public String toString(){
        return this.resourceDescription;
    }
    // </editor-fold>
}   
